<?php

namespace app\http\middleware;

use app\facade\Token;
use think\Db;
use app\models\{User as Users, UserLevel, Riderlist};
use app\facade\Redis;

class Mapi
{
    use \traits\controller\Jump;

    public function handle($request, \Closure $next)
    {
        $url = '/' . $request->controller() . '/' . $request->action();
        $url = strtolower($url);
        $verify = config('auth.api');
        if (!in_array($url, $verify) && !$request->header('token')) {
            $this->result(null, 999, '请授权登陆账号');
        }

        $ip = $request->ip();
        $code = $ip;
        //师傅端
        if (($request->header('tokens') && !in_array($url, $verify))) {
            $user_id = Token::get($request->header('tokens'));
            $prefix = substr($request->header('tokens'), 0, 5);
            if ($prefix == 'RIDER') {
                $user = (new Riderlist())->where('id', $user_id)->find();
            } else {
                $this->result(null, 995, '账号错误');
            }
            $code = $user_id;
            if (!$user) {
                $this->result(null, 995, '账号错误');
            }
            $request->reder = $user;
            $request->reder_id = $user_id;
        }
        return $next($request);
    }
}
